<?php
class site_k_document extends k_Document {
  protected $navigation = array();
  protected $sidebar = array();
  protected $topmenu = array();
  protected $navpath=array();
  protected $headerStack=array();
  protected $header=array();
  protected $root=null;
  
  function navigation() {
    return $this->navigation;
  }
  function sidebar() {
    return $this->sidebar;
  }
  function addSidebar($sidebar) {
    return $this->sidebar[] = $sidebar;
  }
  function addNavigation($level,$data=null){
  	if(is_null($data)) {
  		unset($this->navigation[$level]);
		return;
  	}
  	$this->navigation[$level]=$data;
  }
  function root($root=0){
  	if(!is_null($root)) $this->root=$root;
	return $this->root;
  }
  function getNavObjects($level=null){
  	if(is_null($level)) {
  		$result=array();
		foreach($this->navigation as $level=>$obj){
			$result[$level]=$obj->childNodes();
		}
		return $result;
  	}
  	if(isset($this->navigation[$level])){
  		return $this->navigation[$level]->childNodes();
  	}
  	return false;
  }
	function topmenu(){
		//ss_error::log($this->navigation,'Navigtion');
		$result=array();
		for($i=0;$i<2;$i++){
			if($lvl=$this->getNavObjects($i)){
				$result[$i]=$lvl;
			}
		}
		//ss_error::log($result,'Result');
		array_unshift($result[0],$this->root);
		//array_unshift($result,$this->root);
		//$result[0][0]=$this->root;
		return $result;
	}
	
	public function addHeader($data,$method=1,$recurse=false){
		//ss_error::log($data,'Setting header');
		if($recurse){
			$this->header=array();
			switch($method){
				case 1:
					$this->headerStack=array($data);
					break;
				case 2:
					$this->headerStack[]=$data;
					break;
			}
		} else {
			switch($method){
				case 0:
					$this->header=array();
					break;
				case 1:
					$this->header=$data;
					break;
				case 2:
					$this->header=array($data);
					break;
			}
		}
	}
	
	function header(){
		//ss_error::log($this->headerStack,'Stack');
		//ss_error::log($this->header,'Header');
		if(!is_array($this->header)){
			return $this->header;
		} else {
			return implode('',array_merge($this->headerStack,$this->header));
		}
	}
}
?>